package edu.scau.cxq.gp.common.result;

import lombok.Data;

import java.io.Serializable;

@Data
public class MyResult implements Serializable {

  private static final long serialVersionUID = 1L;


  /**
   * 返回码
   */
  private Integer code;

  /**
   * 返回消息
   */
  private String message;

  /**
   * 返回数据
   */
  private Object data;

  private MyResult() {

  }

  public MyResult(ResultCode resultCode, Object data) {
    this.code = resultCode.code();
    this.message = resultCode.message();
    this.data = data;
  }

  public static MyResult fail(int code) {
    MyResult result = new MyResult();
    result.setCode(code);
    return result;
  }

    private void setResultCode(ResultCode resultCode) {
    this.code = resultCode.code();
    this.message = resultCode.message();
  }

  // 返回成功
  public static MyResult success() {
    MyResult result = new MyResult();
    result.setResultCode(ResultCode.SUCCESS);
    return result;
  }
  // 返回成功
  public static MyResult success(Object data) {
    MyResult result = new MyResult();
    result.setResultCode(ResultCode.SUCCESS);
    result.setData(data);
    return result;
  }

  // 返回失败
  public static MyResult fail(Integer code, String message) {
    MyResult result = new MyResult();
    result.setCode(code);
    result.setMessage(message);
    return result;
  }
  // 返回失败
  public static MyResult fail(ResultCode resultCode) {
    MyResult result = new MyResult();
    result.setResultCode(resultCode);
    return result;
  }
}
